package com.gome.ss.flowcrawl.extractor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;


public class HTMLCount
{
	public  HashMap<String, Integer> filtDict = new HashMap<String, Integer>();
	public String mDictName = "";
	public HTMLCount()
	{
		
	}
	
	public HTMLCount(String fileName)
	{
		mDictName = fileName;
	}
	
	public void writeDict()
    {
		try 
		{
			FileOutputStream out = new FileOutputStream(mDictName);
			for(Entry< String, Integer > entry : filtDict.entrySet())
			{
				String k = String.valueOf(entry.getKey());
				Integer v = entry.getValue();
				String val = k  + "\t" + v + "\n";
				out.write(val.getBytes("utf-8"));
			}
	    	out.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
    }
	
	public  void countNode(Element e)
	{
		Elements es = e.children();
		String tagName = "";
		String text = "";
		String key = "";
	
		tagName = e.tagName();
		text = e.text();

		Attributes as = e.attributes();
		
		String url = "";
		for(Attribute a : as)
		{
			url = a.getValue();
			if(url.indexOf("http")!=-1)
			{
				if(filtDict.containsKey(url))
				{
					int cnt = filtDict.get(url);
					filtDict.put(url, cnt+1);
				}
				else
				{
					filtDict.put(url, 1);
				}
			}
		}
		
		if(text.length()!=0)
		{
			key = tagName + "\t" + text;
			if(filtDict.containsKey(key))
			{
				int cnt = filtDict.get(key);
				filtDict.put(key, cnt+1);
			}
			else
			{
				filtDict.put(key, 1);
			}
		}
		
		if(es.size()!=0)
		{
			for(Element en : es)
			{
				countNode(en);
			}
		}
	}
	
	public void clear()
	{
		filtDict.clear();
	}
	
	public void count(String input)
	{
		Document doc = Jsoup.parse(input);
		Elements e = doc.getElementsByTag("body");
		countNode(e.get(0));
	}
	
	public  void countNode2(Element e, List<String> ls)
	{
		Elements es = e.children();
		String tagName = "";
		String text = "";
		String key = "";
		String url = "";
	
		if(es.size()==0)
		{
			tagName = e.tagName();
			text = e.text();
			
			if(text.length()!=0)
			{
				key = tagName + "\t" + text;
				ls.add(text);
			}
		}
		else 
		{
			for(Element en : es)
			{
				countNode2(en, ls);
			}
		}
	}
	
	
	
	public  List<String> count2(String input)
	{
		List<String> ls = new ArrayList();
		Document doc = Jsoup.parse(input);
		Elements e = doc.getElementsByTag("body");
		countNode2(e.get(0), ls);
		
		return ls;
	}
}
